/*
 * @Author: ZhanXie
 * @Date: 2020-09-17 22:33:16
 * @LastEditors: ZhanXie
 * @LastEditTime: 2020-09-18 13:29:57
 * @Description: koa2-test
 * @FilePath: /projectStudy/node-demo/koa2-test/app.js
 */
// const Koa = require('koa');
const Koa = require('./lib/like-koa2')
const app = new Koa();

// logger

app.use(async (ctx, next) => {
  console.log('洋葱圈第一层开始')
  await next();
  const rt = ctx['X-Response-Time'];
  console.log(`${ctx.req.method} ${ctx.req.url} - ${rt}`);
  console.log('洋葱圈第一层结束')
});

// x-response-time

app.use(async (ctx, next) => {
  console.log('洋葱圈第二层开始')
  const start = Date.now();
  await next();
  const ms = Date.now() - start;
  ctx['X-Response-Time'] = `${ms}ms`;
  console.log('洋葱圈第二层结束')
});

// response

app.use(async ctx => {
  console.log('洋葱圈第三层开始')
  ctx.body = 'Hello World';
  console.log('洋葱圈第三层结束')
});

app.listen(3000);